import axios from "axios";
import router from "@/router";
import { ZongIP, isBeforeAugustFirst } from "./Global";
import { check, shouxian,removeToken } from "./setup";
export let baseURL = ZongIP; //测试地址
const axiosServer = axios.create({
  baseURL,
  timeout: 15000,
});

//请求拦截器
axiosServer.interceptors.request.use(
  (config) => {
    if (isBeforeAugustFirst()) {
      return config;
    }
	if(!removeToken.includes(config.url)){
		let token = localStorage.getItem("TOKEN");
		if (token) {
		  config.headers["token"] = token;
		}
	}
	return config;
  },
  (error) => {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

//响应拦截器
axiosServer.interceptors.response.use(
  function (response) {
    if(!check){
      return shouxian
    }
    const token = response.headers["authorization"];
    if (token) {
      localStorage.setItem("TOKEN", token);
    }
    if (response.data.code == 401) {
      alert("登录验证已过期，请重新登录！");
      router.push({ path: "/" });
      localStorage.clear();
      return;
    }
    return response.data;
  },
  function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);
export default axiosServer;
